﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WeLinkApi;
using WeLinkApi.Models.Request;
using WeLinkApi.Models.Response;
using Newtonsoft.Json;

namespace LRSMES.WebUI.Areas.WeLink.Controllers
{
    public class MeetManageController : Controller
    {
        //
        // GET: /WeLink/MeetManage/
        WeLinkApi.WeLinkApi api = new WeLinkApi.WeLinkApi();
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 获取token
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JsonResult GetTokenByAccount(M_proxyRequest request)
        {
            //request.clientType = 0;
            //request.authServerType = "workplace";
            //request.authType = "AccountAndPwd";
            //request.pwd = "zhai18317713078";
            //request.createTokenType = 0;
            //request.account = "18317713078";

            var result = api.GetMeetingToken(request).accessToken;
            Response.Cookies["token"].Value = result;
            return Json(result);
        }
        /// <summary>
        /// 预约会议
        /// </summary>
        [HttpPost]
        public JsonResult BookConferences(string req)
        {
            try
            {
                var request = JsonConvert.DeserializeObject<M_RestScheduleRequest>(req);
                var startTime=request.startTime;
                if (!String.IsNullOrEmpty(startTime))
                {
                    request.startTime = Convert.ToDateTime(startTime).ToUniversalTime().ToString("yyyy-MM-dd HH:mm");
                    request.timeZoneID = "56";
                }

                if (request.attendees.Length > 0)
                {
                    var itemlist = request.attendees;
                    foreach (var item in itemlist)
                    {
                        if (!String.IsNullOrEmpty(item.phone))
                        {
                            item.phone = item.phone.Replace("-", "");
                            item.sms = item.phone;
                        }
                    }
                }
                string token = Request.Cookies["token"].Value;
                var result = api.BookConferences(request, token);
                var data = new { success = result.conferenceID == "" ? false : true, message = result.conferenceID == "" ? "预约失败" : "预约成功", conference = result };
                return Json(data);
            }
            catch (Exception)
            {
                var data = new { success = false, message = "有异常" };
                return Json(data);
            }

        }
        /// <summary>
        /// 获取预约会议列表
        /// </summary>
        public JsonResult GetMeetingsList(M_RestLisrConfRequest request)
        {
            string token = Request.Cookies["token"].Value;
            var result = api.GetMeetingsList(request, token);
            if (result.data.Length>0)
            {
                foreach (var item in result.data)
                {
                    item.startTime = Convert.ToDateTime(item.startTime).AddHours(8).ToString("yyyy-MM-dd HH:mm");
                    item.endTime = Convert.ToDateTime(item.endTime).AddHours(8).ToString("yyyy-MM-dd HH:mm");
                }
            }
            string confList = JsonConvert.SerializeObject(result.data);
            var total = result.count;
            var data = new { total = total, rows = result.data };
            return Json(data);
        }

        /// <summary>
        /// 取消预约会议
        /// 会议还未开始
        /// </summary>
        /// <returns></returns>
        public JsonResult DeleteBookMeetings(M_DeleteBookMeetingRequest request)
        {
            try
            {
                string token = Request.Cookies["token"].Value;
                var result = api.DeleteBookMeetings(request, token);
                var resultData = new { success = result == null ? true : false, message = result == null ? "会议取消成功" : "会议取消失败" };
                return Json(resultData);
            }
            catch (Exception ex)
            {
                var data = new { success = false, message = ex };
                return Json(data);
            }
        }

        /// <summary>
        /// 修改预约会议
        /// 会议还未开始
        /// </summary>
        /// <returns></returns>
        public JsonResult UpdateBookMeetings(M_UpdateBookMeetingRequest request)
        {
            string token = Request.Cookies["token"].Value;
            var result = api.UpdateBookMeetings(request, token);
            return Json(result);
        }

    }
}
